\hypertarget{ftp__server_8c}{}\doxysection{ftp\+\_\+server.\+c File Reference}
\label{ftp__server_8c}\index{ftp\_server.c@{ftp\_server.c}}
{\ttfamily \#include $<$pthread.\+h$>$}\newline
{\ttfamily \#include $<$errno.\+h$>$}\newline
{\ttfamily \#include $<$stdio.\+h$>$}\newline
{\ttfamily \#include $<$ctype.\+h$>$}\newline
{\ttfamily \#include $<$stdlib.\+h$>$}\newline
{\ttfamily \#include $<$unistd.\+h$>$}\newline
{\ttfamily \#include $<$strings.\+h$>$}\newline
{\ttfamily \#include $<$string.\+h$>$}\newline
{\ttfamily \#include $<$fcntl.\+h$>$}\newline
{\ttfamily \#include $<$sys/socket.\+h$>$}\newline
{\ttfamily \#include $<$sys/types.\+h$>$}\newline
{\ttfamily \#include $<$sys/stat.\+h$>$}\newline
{\ttfamily \#include $<$netinet/in.\+h$>$}\newline
{\ttfamily \#include $<$arpa/inet.\+h$>$}\newline
{\ttfamily \#include $<$time.\+h$>$}\newline
{\ttfamily \#include \char`\"{}ftp\+\_\+server.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}config.\+h\char`\"{}}\newline
{\ttfamily \#include \char`\"{}cmd\+\_\+server.\+h\char`\"{}}\newline
Include dependency graph for ftp\+\_\+server.\+c\+:
\nopagebreak
\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=350pt]{ftp__server_8c__incl}
\end{center}
\end{figure}
\doxysubsection*{Macros}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{ftp__server_8c_a4030f144b22022d7463211b78c592383}\label{ftp__server_8c_a4030f144b22022d7463211b78c592383}} 
\#define {\bfseries FTP\+\_\+\+SERVER}
\item 
\mbox{\Hypertarget{ftp__server_8c_ac4d8afc60a6a97fac2ee94e1def2a267}\label{ftp__server_8c_ac4d8afc60a6a97fac2ee94e1def2a267}} 
\#define {\bfseries FTP\+\_\+\+SERVER\+\_\+\+PORT}~21
\item 
\mbox{\Hypertarget{ftp__server_8c_a4590bc240c9d6d928eecc7484553c2e7}\label{ftp__server_8c_a4590bc240c9d6d928eecc7484553c2e7}} 
\#define {\bfseries USER\+\_\+\+MODE}~1
\item 
\mbox{\Hypertarget{ftp__server_8c_aaf7aa7762a0536af133a25d4d2b96be6}\label{ftp__server_8c_aaf7aa7762a0536af133a25d4d2b96be6}} 
\#define {\bfseries ANONYMOUS\+\_\+\+MODE}~2
\end{DoxyCompactItemize}
\doxysubsection*{Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{ftp__server_8c_afe95cbf25cb718882af3b270d8444891}\label{ftp__server_8c_afe95cbf25cb718882af3b270d8444891}} 
void {\bfseries greetings} (void)
\begin{DoxyCompactList}\small\item\em server的欢迎语。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__server_8c_a4297bbb7ddb30e7cc2c896d127501eb5}{create\+\_\+socket}} (void)
\begin{DoxyCompactList}\small\item\em 创建一个\+TCP套接字。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_a717154ba6392e7882d105c8878e2b89e}{bind\+\_\+to\+\_\+port}} (struct sockaddr\+\_\+in server\+\_\+addr, int sockfd)
\begin{DoxyCompactList}\small\item\em 绑定到指定端口上。若绑定不成功，将会直接退出程序。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_a0cc55115a0b678b249e3bc9044f9d79b}{listen\+\_\+to\+\_\+port}} (int sockfd, int backlog)
\begin{DoxyCompactList}\small\item\em 使套接字监听本地指定端口。若监听失败，将直接退出。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__server_8c_a7ff013fbdf95b96f2d023fec829a0ef5}{accept\+\_\+connections}} (int sock\+\_\+listen)
\begin{DoxyCompactList}\small\item\em 在当前监听的本地端口，接受来自外部的连接请求。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_a80762bccbf172f3bcc3c6149c6c407c5}{send\+\_\+message}} (int socket\+\_\+control, char $\ast$info, char $\ast$extra)
\begin{DoxyCompactList}\small\item\em 向客户端发送信息。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__server_8c_a51e4c16da2d4948788531008dd381d35}{recv\+\_\+message}} (int socket\+\_\+control)
\begin{DoxyCompactList}\small\item\em 接收客户端信息。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__server_8c_a79275ba100d8e41616e85d2640b4ccdb}{validate\+\_\+username}} (char $\ast$username)
\begin{DoxyCompactList}\small\item\em 验证给出的用户名是否存在。 \end{DoxyCompactList}\item 
int \mbox{\hyperlink{ftp__server_8c_a05cb29dcebf6f6367e907c50b507177a}{validate\+\_\+password}} (char $\ast$password, int mode)
\begin{DoxyCompactList}\small\item\em 验证给出的密码是否匹配用户名。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_a46870c32ba12c8313bcb7664e55c13ca}{user\+\_\+login}} (int socket\+\_\+control)
\begin{DoxyCompactList}\small\item\em 处理用户登入的模块。如果用户没有正确选择以匿名或以帐户模式登录，则一直请求用户输入帐号密码，否则不允许后续操作。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_a3e49a1da2caece9751648f488b1519c3}{command\+\_\+handler}} (int socket\+\_\+control)
\begin{DoxyCompactList}\small\item\em 反复处理用户端发送的指令。 \end{DoxyCompactList}\item 
void \mbox{\hyperlink{ftp__server_8c_aaf5d9aef5682b7dbbc48cdc1833b74ea}{handle\+\_\+client\+\_\+request}} (int socket\+\_\+control)
\begin{DoxyCompactList}\small\item\em 处理用户的请求，包括登录和命令处理。 \end{DoxyCompactList}\item 
\mbox{\Hypertarget{ftp__server_8c_a0ddf1224851353fc92bfbff6f499fa97}\label{ftp__server_8c_a0ddf1224851353fc92bfbff6f499fa97}} 
int {\bfseries main} (int argc, char $\ast$argv\mbox{[}$\,$\mbox{]})
\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{ftp__server_8c_abdcbc981b6a34e3bced2044702eb1f2c}\label{ftp__server_8c_abdcbc981b6a34e3bced2044702eb1f2c}} 
\mbox{\hyperlink{structMsg}{Msg}} {\bfseries msg}
\item 
\mbox{\Hypertarget{ftp__server_8c_a4f06d94960b8db675b7855a790c93efb}\label{ftp__server_8c_a4f06d94960b8db675b7855a790c93efb}} 
struct sockaddr\+\_\+in server\+\_\+addr client\+\_\+addr {\bfseries addr\+\_\+data}
\item 
\mbox{\Hypertarget{ftp__server_8c_ab1fb5eb1a13a99ada213f0bcd7700de6}\label{ftp__server_8c_ab1fb5eb1a13a99ada213f0bcd7700de6}} 
char {\bfseries buffer} \mbox{[}MAX\+\_\+\+INFO\mbox{]}
\item 
\mbox{\Hypertarget{ftp__server_8c_af94de09503c48ba86a464e6b24b866d7}\label{ftp__server_8c_af94de09503c48ba86a464e6b24b866d7}} 
char {\bfseries local\+\_\+user} \mbox{[}$\,$\mbox{]} = \char`\"{}admin\char`\"{}
\item 
\mbox{\Hypertarget{ftp__server_8c_a1959ae9658902fc8502579dda8063029}\label{ftp__server_8c_a1959ae9658902fc8502579dda8063029}} 
char {\bfseries local\+\_\+password} \mbox{[}$\,$\mbox{]} = \char`\"{}123\char`\"{}
\item 
\mbox{\Hypertarget{ftp__server_8c_a317593e04243f55ebc5c17e8a9c0bf70}\label{ftp__server_8c_a317593e04243f55ebc5c17e8a9c0bf70}} 
char {\bfseries anony\+\_\+user} \mbox{[}$\,$\mbox{]} = \char`\"{}anonymous\char`\"{}
\item 
\mbox{\Hypertarget{ftp__server_8c_a225cc4d96fd727d76775941bbb0dde0f}\label{ftp__server_8c_a225cc4d96fd727d76775941bbb0dde0f}} 
char {\bfseries anony\+\_\+password} \mbox{[}$\,$\mbox{]} = \char`\"{}\char`\"{}
\item 
\mbox{\Hypertarget{ftp__server_8c_a18dc7c290b054bb13a75634c1400d58d}\label{ftp__server_8c_a18dc7c290b054bb13a75634c1400d58d}} 
int {\bfseries ftp\+\_\+data\+\_\+sock}
\item 
\mbox{\Hypertarget{ftp__server_8c_a1ea5d0cb93f22f7d0fdf804bd68c3326}\label{ftp__server_8c_a1ea5d0cb93f22f7d0fdf804bd68c3326}} 
int {\bfseries mode} = -\/1
\end{DoxyCompactItemize}


\doxysubsection{Detailed Description}
Code for ftp server.

\begin{DoxyAuthor}{Author}
sc 
\end{DoxyAuthor}
\begin{DoxyVersion}{Version}
V1.\+0 
\end{DoxyVersion}
\begin{DoxyDate}{Date}
2022-\/11-\/19 
\end{DoxyDate}
\begin{DoxyCopyright}{Copyright}
GNU Public License V3.\+0 
\end{DoxyCopyright}


\doxysubsection{Function Documentation}
\mbox{\Hypertarget{ftp__server_8c_a7ff013fbdf95b96f2d023fec829a0ef5}\label{ftp__server_8c_a7ff013fbdf95b96f2d023fec829a0ef5}} 
\index{ftp\_server.c@{ftp\_server.c}!accept\_connections@{accept\_connections}}
\index{accept\_connections@{accept\_connections}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{accept\_connections()}{accept\_connections()}}
{\footnotesize\ttfamily int accept\+\_\+connections (\begin{DoxyParamCaption}\item[{int}]{sock\+\_\+listen }\end{DoxyParamCaption})}



在当前监听的本地端口，接受来自外部的连接请求。 


\begin{DoxyParams}{Parameters}
{\em sock\+\_\+listen} & 监听本地端口的套接字的描述符。 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
成功则返回套接字描述符，失败则报错并返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__server_8c_a717154ba6392e7882d105c8878e2b89e}\label{ftp__server_8c_a717154ba6392e7882d105c8878e2b89e}} 
\index{ftp\_server.c@{ftp\_server.c}!bind\_to\_port@{bind\_to\_port}}
\index{bind\_to\_port@{bind\_to\_port}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{bind\_to\_port()}{bind\_to\_port()}}
{\footnotesize\ttfamily void bind\+\_\+to\+\_\+port (\begin{DoxyParamCaption}\item[{struct sockaddr\+\_\+in}]{server\+\_\+addr,  }\item[{int}]{sockfd }\end{DoxyParamCaption})}



绑定到指定端口上。若绑定不成功，将会直接退出程序。 


\begin{DoxyParams}{Parameters}
{\em server\+\_\+addr} & 要绑定的本地端口 \\
\hline
{\em sockfd} & 要执行绑定的套接字 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a3e49a1da2caece9751648f488b1519c3}\label{ftp__server_8c_a3e49a1da2caece9751648f488b1519c3}} 
\index{ftp\_server.c@{ftp\_server.c}!command\_handler@{command\_handler}}
\index{command\_handler@{command\_handler}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{command\_handler()}{command\_handler()}}
{\footnotesize\ttfamily void command\+\_\+handler (\begin{DoxyParamCaption}\item[{int}]{socket\+\_\+control }\end{DoxyParamCaption})}



反复处理用户端发送的指令。 


\begin{DoxyParams}{Parameters}
{\em socket\+\_\+control} & 发送控制信令的套接字描述符。 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a4297bbb7ddb30e7cc2c896d127501eb5}\label{ftp__server_8c_a4297bbb7ddb30e7cc2c896d127501eb5}} 
\index{ftp\_server.c@{ftp\_server.c}!create\_socket@{create\_socket}}
\index{create\_socket@{create\_socket}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{create\_socket()}{create\_socket()}}
{\footnotesize\ttfamily int create\+\_\+socket (\begin{DoxyParamCaption}\item[{void}]{ }\end{DoxyParamCaption})}



创建一个\+TCP套接字。 

\begin{DoxyReturn}{Returns}
成功则返回套接字的描述符，失败则返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__server_8c_aaf5d9aef5682b7dbbc48cdc1833b74ea}\label{ftp__server_8c_aaf5d9aef5682b7dbbc48cdc1833b74ea}} 
\index{ftp\_server.c@{ftp\_server.c}!handle\_client\_request@{handle\_client\_request}}
\index{handle\_client\_request@{handle\_client\_request}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{handle\_client\_request()}{handle\_client\_request()}}
{\footnotesize\ttfamily void handle\+\_\+client\+\_\+request (\begin{DoxyParamCaption}\item[{int}]{socket\+\_\+control }\end{DoxyParamCaption})}



处理用户的请求，包括登录和命令处理。 


\begin{DoxyParams}{Parameters}
{\em socket\+\_\+control} & 发送控制信令的套接字描述符。 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a0cc55115a0b678b249e3bc9044f9d79b}\label{ftp__server_8c_a0cc55115a0b678b249e3bc9044f9d79b}} 
\index{ftp\_server.c@{ftp\_server.c}!listen\_to\_port@{listen\_to\_port}}
\index{listen\_to\_port@{listen\_to\_port}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{listen\_to\_port()}{listen\_to\_port()}}
{\footnotesize\ttfamily void listen\+\_\+to\+\_\+port (\begin{DoxyParamCaption}\item[{int}]{sockfd,  }\item[{int}]{backlog }\end{DoxyParamCaption})}



使套接字监听本地指定端口。若监听失败，将直接退出。 


\begin{DoxyParams}{Parameters}
{\em sockfd} & 将要进行监听的套接字。 \\
\hline
{\em backlog} & 最多允许接收的连接请求数。 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a51e4c16da2d4948788531008dd381d35}\label{ftp__server_8c_a51e4c16da2d4948788531008dd381d35}} 
\index{ftp\_server.c@{ftp\_server.c}!recv\_message@{recv\_message}}
\index{recv\_message@{recv\_message}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{recv\_message()}{recv\_message()}}
{\footnotesize\ttfamily int recv\+\_\+message (\begin{DoxyParamCaption}\item[{int}]{socket\+\_\+control }\end{DoxyParamCaption})}



接收客户端信息。 


\begin{DoxyParams}{Parameters}
{\em socket\+\_\+control} & 发送控制信令的套接字描述符。 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
若接收成功，则返回信息长度；失败则返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__server_8c_a80762bccbf172f3bcc3c6149c6c407c5}\label{ftp__server_8c_a80762bccbf172f3bcc3c6149c6c407c5}} 
\index{ftp\_server.c@{ftp\_server.c}!send\_message@{send\_message}}
\index{send\_message@{send\_message}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{send\_message()}{send\_message()}}
{\footnotesize\ttfamily void send\+\_\+message (\begin{DoxyParamCaption}\item[{int}]{socket\+\_\+control,  }\item[{char $\ast$}]{info,  }\item[{char $\ast$}]{extra }\end{DoxyParamCaption})}



向客户端发送信息。 


\begin{DoxyParams}{Parameters}
{\em socket\+\_\+control} & 发送控制信令的套接字描述符。 \\
\hline
{\em info} & 要发送的信息（通常是预先定义好的\+RFC标准应答） \\
\hline
{\em extra} & 附加信息 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a46870c32ba12c8313bcb7664e55c13ca}\label{ftp__server_8c_a46870c32ba12c8313bcb7664e55c13ca}} 
\index{ftp\_server.c@{ftp\_server.c}!user\_login@{user\_login}}
\index{user\_login@{user\_login}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{user\_login()}{user\_login()}}
{\footnotesize\ttfamily void user\+\_\+login (\begin{DoxyParamCaption}\item[{int}]{socket\+\_\+control }\end{DoxyParamCaption})}



处理用户登入的模块。如果用户没有正确选择以匿名或以帐户模式登录，则一直请求用户输入帐号密码，否则不允许后续操作。 


\begin{DoxyParams}{Parameters}
{\em socket\+\_\+control} & 发送控制信令的套接字描述符。 \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{ftp__server_8c_a05cb29dcebf6f6367e907c50b507177a}\label{ftp__server_8c_a05cb29dcebf6f6367e907c50b507177a}} 
\index{ftp\_server.c@{ftp\_server.c}!validate\_password@{validate\_password}}
\index{validate\_password@{validate\_password}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{validate\_password()}{validate\_password()}}
{\footnotesize\ttfamily int validate\+\_\+password (\begin{DoxyParamCaption}\item[{char $\ast$}]{password,  }\item[{int}]{mode }\end{DoxyParamCaption})}



验证给出的密码是否匹配用户名。 


\begin{DoxyParams}{Parameters}
{\em password} & 客户端输入的密码 \\
\hline
{\em mode} & 登录模式 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
存在则返回登录模式，否则返回-\/1。 
\end{DoxyReturn}
\mbox{\Hypertarget{ftp__server_8c_a79275ba100d8e41616e85d2640b4ccdb}\label{ftp__server_8c_a79275ba100d8e41616e85d2640b4ccdb}} 
\index{ftp\_server.c@{ftp\_server.c}!validate\_username@{validate\_username}}
\index{validate\_username@{validate\_username}!ftp\_server.c@{ftp\_server.c}}
\doxysubsubsection{\texorpdfstring{validate\_username()}{validate\_username()}}
{\footnotesize\ttfamily int validate\+\_\+username (\begin{DoxyParamCaption}\item[{char $\ast$}]{username }\end{DoxyParamCaption})}



验证给出的用户名是否存在。 


\begin{DoxyParams}{Parameters}
{\em username} & 客户端输入的用户名 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
存在则返回登录模式，否则返回-\/1。 
\end{DoxyReturn}
